﻿/*
Copyright (c) 2010, hiSoft
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the hiSoft nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE HISOFT AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL HISOFT BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
using System;
using System.Threading;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Orchard.Test.Library;
using Selenium;

namespace Orchard.Test.WorkThoughCases.Auto
{
    [TestClass]
    public class DoActionToComments
    {
        private TestLibrary TestLibrary;
        private ISelenium selenium;

        [TestInitialize]
        public void SetupTest()
        {
            TestLibrary = TestLibrary.SetupTest(this.ToString());
            selenium = TestLibrary.Selenium;
        }

        [TestCleanup]
        public void TeardownTest()
        {
            TestLibrary.ShutDown();
        }

        [TestMethod]
        public void WorkThoughCases_DoActionToComments()
        {
            TestLibrary.UserHelper.LogOnAsAdmin();
            TestLibrary.BlogHelper.ViewDefaultTestPost();
            var comment1=TestLibrary.CommentHelper.CommentOn(TestLibrary.DefaultPost,true,"This is a test comment by admin at "+ DateTime.Now.ToString());
            Thread.Sleep(3000);
            Assert.IsTrue(TestLibrary.CommentHelper.IsCommentPresent(comment1));
            TestLibrary.UserHelper.LogOff();
            string comText="This is a test comment without authenticated at " + DateTime.Now.ToString();
            string commentUrl = "http://www.google.com";
            var comment2 = TestLibrary.CommentHelper.CommentOn(TestLibrary.DefaultPost, comText, "Wang", "wang2@hisoft.com", commentUrl);
            //Assert.IsTrue(TestLibrary.CommentHelper.IsCommentPresent(comment2)); //can not use this function to check if comment is exist when has not yet logon
            selenium.Open("/default-test-blog/default-test-post");
            Assert.IsTrue(selenium.IsTextPresent(comText));

            TestLibrary.UserHelper.LogOnAsAdmin();
            selenium.Open("/Admin");
            TestLibrary.CommentHelper.ManageComments();
            TestLibrary.CommentHelper.DoSelectActionToComment(comment1, CommentAction.MarkAsSpam);
            Assert.IsTrue(TestLibrary.CommentHelper.IsCommentStatusExpected(comment1, CommentStatus.Spam));
            TestLibrary.CommentHelper.DoSelectActionToComment(comment1, CommentAction.Approve);
            Assert.IsTrue(TestLibrary.CommentHelper.IsCommentStatusExpected(comment1, CommentStatus.Approved));
            TestLibrary.CommentHelper.DoSelectActionToComment(comment1, CommentAction.Unapprove);
            Assert.IsTrue(TestLibrary.CommentHelper.IsCommentStatusExpected(comment1, CommentStatus.Pending));
            TestLibrary.CommentHelper.DoSelectActionToComment(comment1, CommentAction.Delete);
            Assert.IsFalse(TestLibrary.CommentHelper.IsCommentPresent(comment1));

            var comment_1 = TestLibrary.CommentHelper.CommentOn(TestLibrary.DefaultPost, true, "This is a test comment by admin at " + DateTime.Now.ToString());
            TestLibrary.CommentHelper.DoActionToComment(comment_1, CommentAction.MarkAsSpam);
            Assert.IsTrue(TestLibrary.CommentHelper.IsCommentStatusExpected(comment_1, CommentStatus.Spam));
            TestLibrary.CommentHelper.DoActionToComment(comment_1, CommentAction.Approve);
            Assert.IsTrue(TestLibrary.CommentHelper.IsCommentStatusExpected(comment_1, CommentStatus.Approved));
            TestLibrary.CommentHelper.DoActionToComment(comment_1, CommentAction.Unapprove);
            Assert.IsTrue(TestLibrary.CommentHelper.IsCommentStatusExpected(comment_1, CommentStatus.Pending));
            TestLibrary.CommentHelper.DoActionToComment(comment_1, CommentAction.Delete);
            Assert.IsFalse(TestLibrary.CommentHelper.IsCommentPresent(comment_1));

            string commentText = string.Format("This is a test comment for check Edit Comment at " + DateTime.Now.ToString());
            TestLibrary.CommentHelper.EditComment(comment2, "wang", "wang2@hisoft.com", commentUrl, commentText, CommentAction.MarkAsSpam);
            Assert.IsTrue(TestLibrary.CommentHelper.IsCommentStatusExpected(comment2, CommentStatus.Spam));
            TestLibrary.CommentHelper.EditComment(comment2, "wang", "wang2@hisoft.com", commentUrl, commentText, CommentAction.Approve);
            Assert.IsTrue(TestLibrary.CommentHelper.IsCommentStatusExpected(comment2, CommentStatus.Approved));
            TestLibrary.CommentHelper.EditComment(comment2, "wang", "wang2@hisoft.com", commentUrl, commentText, CommentAction.Unapprove);
            Assert.IsTrue(TestLibrary.CommentHelper.IsCommentStatusExpected(comment2, CommentStatus.Pending));

            TestLibrary.SettingHelper.ConfigureCommentsSettings(true, false, "", "");
            TestLibrary.UserHelper.LogOff();
            var comment3 = TestLibrary.CommentHelper.CommentOn(TestLibrary.DefaultPost, false, "This is a test comment need approved befor appear by administator");
            TestLibrary.UserHelper.LogOnAsAdmin();
            Assert.IsTrue(TestLibrary.CommentHelper.IsCommentStatusExpected(comment3, CommentStatus.Pending),"Bug ? (Please check this case,and check if settings can not be saved)");
            TestLibrary.CommentHelper.DoActionToComment(comment3, CommentAction.Approve);
            Assert.IsTrue(TestLibrary.CommentHelper.IsCommentStatusExpected(comment3, CommentStatus.Approved));

            selenium.Open("/Admin");
            selenium.Click("//a[contains(@href, '/Admin/Settings/Comments')]");
            TestLibrary.SettingHelper.ConfigureCommentsSettings(false, true, "default-test-blog", "default-test-blog");
            TestLibrary.UserHelper.LogOff();
            var comment4=TestLibrary.CommentHelper.CommentOn(TestLibrary.DefaultPost, false, "This is a test comment by spam protection");
            TestLibrary.UserHelper.LogOnAsAdmin();
            Assert.IsTrue(TestLibrary.CommentHelper.IsCommentStatusExpected(comment4, CommentStatus.Spam));

        }
    }
}
